// pages/transferStoreList.js
import { queryIndustryList } from '../../api/common'
import { queryTransferShopList } from '../../api/shop'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    industryId: '',
    filterOptions: [
      {
        id: 'more',
        name: '更多'
      }
    ],
    activeTab: '1',
    tabOptions: [
      {
        id: '1',
        name: '推荐'
      },
      {
        id: '2',
        name: '上新'
      },
      {
        id: '3',
        name: '急转'
      },
      {
        id: '4',
        name: '热板'
      },
    ],
    industryValue: '',
    industryText: '业态',
    industryOptions: [],
    priceValue: '',
    priceOptions: [
      { text: '价格1', value: '1' },
      { text: '价格2', value: '2' },
      { text: '价格3', value: '3' },
    ],
    moreValue: '',
    moreOptions: [
      { text: '更多1', value: '1' },
      { text: '更多2', value: '2' },
      { text: '更多3', value: '3' },
    ],
    adressTitle: '杭州',
    adressValue: '',

    shopList: [],
    isLoading: false,
    isNoMoreData: false,
    pageNum: 1,
    pageSize: 10,
    keywords: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options, 'ooooo');
    this.industryId = options.id;
    this.queryIndustryListFn();
    this.loadShopData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  toFilterHandle(item) {
    console.log(item, '过滤项');
  },

  onChangeIndustry(event) {
    const industryId = event.detail;
    this.setData({
      shopList: [],
      pageNum: 1,
      industryValue: industryId,
      industryText: this.data.industryOptions.find(val=>val.value===industryId).text || '业态'
    })
    this.loadShopData();
  },
  async loadShopData() {
    // const { pageNum, pageSize } = this.data;
    this.setData({ isLoading: true });
    try {
      const params = {
        "page": this.data.pageNum,
        "pageSize": this.data.pageSize,
        "titleLike": "",
        "province": "",
        "city": "",
        "district": "",
        "tradeTypeId": this.industryId,
        "shopTypeId": this.data.industryValue
      }
      const {code, data, message} = await queryTransferShopList(params);
      if(code==='0') {
        if (data.list.length === 0) {
          this.setData({
            isNoMoreData: true,
            isLoading: false
          });
          return;
        }
        this.setData({
          shopList: [...this.data.shopList, ...data.list],
          isLoading: false,
          pageNum: this.data.pageNum + 1
        });
        if (this.data.pageNum > Math.floor(data.total/this.pageSize.data)) {
          this.setData({ isNoMoreData: true });
        }
      }else {
        wx.showToast({
          title: message,
          icon: 'none',
          duration: 2000
        })
      }
    }catch (error) {

    }
  },
  // 获取行业线数据
  async queryIndustryListFn() {
    try {
      const {data, code} = await queryIndustryList({parentId: this.industryId});
      console.log(data, 'rrrrr');
      if(code==='0') {
        let filterIndustryList = [];
        if(data&&data.length>10) {
          filterIndustryList = [...data.slice(0, 9), ...this.data.filterOptions];
        }else {
          filterIndustryList = data;
        }
        this.setData({
          industryOptions: data.map(val=>{
            return {
              value: val.id,
              text: val.name
            }
          }),
          filterOptions: filterIndustryList
        });
      }

    } catch (error) {
      console.log(error, 'error');
    }
  }
})